/**
 * Created by zpc on 2017/12/15.
 */
$(function () {
    getOrderList();
    $("content_box .state_box .logistics").click(function(){
        $(".logistics_box").show();
        $(".background_box").show();
    });
    $(".background_box").click(function(){
        $(this).hide();
        $(".logistics_box").hide();
    });
    $(".logistics_box .title .esc").click(function(){
        $(".logistics_box").hide();
        $(".background_box").hide();
    })
$(".refund_reason .button_box button").click(function () {
var orderId=$(".refund_reason").attr("orderId");
var refundReason=$(".refund_reason .reason_box").html();
    var postData = {orderId: orderId,refundReason:refundReason}
    $.ajax({
        type: "post",
        url: "/order/refund.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            alert("已提交退款申请！");
            $(".refund_reason").hide();
        }

    });
});
    $(".refund_reason .esc").click(function () {
        $(".refund_reason").hide();
    });

})

function setPic(productEntityId, obj) {
    var postData = {resourceRelationId: productEntityId, resourceRelationType: 4}
    $.ajax({
        type: "post",
        url: "/resource/getResourceList.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {

            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var data = res.data;
            var pic = "";
            if (data.length > 0) {
                pic = picPath + data[0].resourceUrl;
                obj.attr('src', pic);
            }
         else
            {
                obj.parent().hide();
            }


            /*if($(".information_box img").attr("src") == ""){
                $(".information_box .img_box").hide()
            }*/
        }

    });
}


function getOrderList() {
    var userId = $("#userId").val();
    var postData = {};
    $.ajax({
        type: "post",
        url: "/order/getOrderList.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }

            showData(res.data.list);
        }
    });
}

function showData(orderList) {
    var html = '';
    console.log(orderList);
    for (var i = 0 ; i < orderList.length; i++ ) {

        console.log();
        html += '<div class="box_1">';
        html += '<div class="top">';
        html += '   <span class="time"> ' + (new Date(orderList[i].createTime)).Format("yyyy-MM-dd") + '</span>';
        html += '   <span class="number">订单号:'+ orderList[i].orderId +'</span>';
        html += '   <span class="state">' + getOrderStatus( orderList[i].status) + '</span>';
        html += '</div>';

        for (var j = 0; j < orderList[i].orderItemList.length; j++) {
            console.log(orderList[i].orderItemList.length);
            html += '<div class="information_box">';
            html += '   <div class="img_box"><img src="" alt="" data-id="' + orderList[i].orderItemList[j].productEntity.productEntityId + '"></div>';
            html += '   <div class="information_box_1">';
            html += '       <h5>'+orderList[i].orderItemList[j].productEntity.productEntityName+'</h5>';
            if(orderList[i].orderItemList[j].productEntity.productEntityColour!=undefined){
                html += '       <h5><span class="color">颜色：</span><span>'+ orderList[i].orderItemList[j].productEntity.productEntityColour +'</span></h5>';
            }

            if(orderList[i].orderItemList[j].productEntity.productEntityColour==undefined){
                html += '       <h5><span class="package">套餐：</span><span>' + orderList[i].orderItemList[j].productEntity.productEntityName + '</span></h5>';
            }


            html += '       <h5>￥&nbsp;' + orderList[i].orderItemList[j].price/100 + '</h5>';
            html += '   </div>';
            html += '</div>';






        }
        html += '<div class="state_box">';

        if (orderList[i].status == 1){
            html += '<a href="javascript:void(0)" class="" onclick="cancelOrder(' + orderList[i].orderId + ');">取消订单</a>';
            html += '<a href="javascript:void(0)" class="btn btn_2" onclick="toPay(' + orderList[i].orderId + ')">付款</a>';
        }
        if (orderList[i].status == 2){
            html += '<a href="javascript:void(0)" class="logistics" onclick="refund(' + orderList[i].orderId + ')">申请退款</a>';
        }
        if (orderList[i].status == 5){
            html += '<a href="javascript:void(0)" class="" onclick="cancelOrder(' + orderList[i].orderId + ');" >删除订单</a>';
            html += '<a href="javascript:void(0)" class="logistics" onclick="checkLogistics(' + orderList[i].orderId + ');">查看物流</a>';
        }

        if (orderList[i].status == 13){

           html += '<a href="javascript:void(0)" class="logistics" onclick="refund(' + orderList[i].orderId + ')">申请退款</a>';
            html += '<a href="javascript:void(0)" class="logistics" onclick="checkLogistics(' + orderList[i].orderId + ');">查看物流</a>';
            html += '<a href="javascript:void(0)" class="logistics" onclick="orderConfirm(' + orderList[i].orderId + ');">确认收货</a>';
        }
        html += '</div>';

        html += '</div>';
    }
    $("#orderList").html(html);
    /*$(".information_box img").each(function () {
        var productEntityId = $(this).attr("data-id");
        setPic(productEntityId, $(this));
    }) ;*/

    $(".information_box img").each(function () {

        var productEntityId = $(this).attr("data-id");
        setPic(productEntityId, $(this));
    })

}

function checkLogistics(orderItemId) {
    var postData = {orderItemId: orderItemId}
    $.ajax({
        type: "post",
        url: "/logistics/checkLogistics.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }


            var info=JSON.parse(res.data);
            var logisticCode=info.LogisticCode;
            var shipperCode=info.ShipperCode;
            var traces=info.Traces;
            console.log(logisticCode);
            console.log(shipperCode);
            console.log(traces);

            var html="";
            for(index in traces){
                var trace=traces[index];
                console.log(trace);
                html+='<div>' + trace.AcceptStation + '</div>';
                html+='<div class="AcceptTime"><span>'+  trace.AcceptTime + '</span></div>';
                html+='<hr/>'

            }

            $(".logistics_box .logistics_information").html(html);
            $(".logistics_box").show();
            $(".background_box").show();
          ///  alert(res.data);
        }

    });
}

function toPay(orderId) {
    location.href = "/pay/" + orderId + ".html"
}
function getOrderStatus(status) {
    var statusStr = ""
    switch (status) {
        case -2:
            statusStr = "删除";
            break;
        case -1:
            statusStr = "已过期";
            break;
        case 0:
            statusStr = "已取消";
            break;
        case 1:
            statusStr = "未支付";
            break;
        case 2:
            statusStr = "已支付";
            break;
        case 5:
            statusStr = "交易完成";
            break;
        case 6:
            statusStr = "申请退款";
            break;
        case 7:
            statusStr = "取消退款";
            break;
        case 8:
            statusStr = "已退款";
            break;
        case 9:
            statusStr = "交易完成";
            break;
        case 12:
            statusStr = "待发货";
            break;
        case 13:
            statusStr = "已发货";
            break;
        case 14:
            statusStr = "已收货";
            break;
        default:
            statusStr = status;
            break;
    }
    return statusStr;
}
function cancelOrder(orderId) {
    var postData = {orderId: orderId}
    $.ajax({
        type: "post",
        url: "/order/cancelOrder.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            alert("订单取消成功！");
        }

    });
}
function refund(orderId) {
    $(".refund_reason").attr("orderId",orderId);
    $(".refund_reason").show();
}
function cancelRefund(orderItemId) {
    var postData = {orderItemId: orderItemId}
    $.ajax({
        type: "post",
        url: "/order/cancelRefund.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            alert("取消退款成功！");
        }

    });
}
function orderConfirm(orderId) {
    var postData = {orderId: orderId}
    $.ajax({
        type: "post",
        url: "/order/orderConfirm.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            alert("确认收货成功！");
        }

    });
}
